package offer;

public class Offer24 {
    public static void main(String[] args) {
        Solution s = new Solution();
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node1.next = node2;
        node2.next = node3;
        ListNode listNode = s.reverseList(node1);
        System.out.println(listNode.val);
        System.out.println(listNode.next.val);
        System.out.println(listNode.next.next.val);
        System.out.println(listNode.next.next.next.val);
    }

    //翻转链表
    //1->2->3->4->5->NULL
    static class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode cursor = head, next = null, newHead = null;
            while (cursor != null) {
                next = cursor.next;//下一个元素
                cursor.next = newHead;
                newHead = cursor;
                cursor = next;//移动下标
            }

            return newHead;
        }
    }

    class Answer {
        public ListNode reverseList(ListNode head) {
            ListNode prev = null;
            ListNode curr = head;
            while (curr != null) {
                ListNode next = curr.next;
                curr.next = prev;
                prev = curr;
                curr = next;
            }
            return prev;
        }
    }
}


